package com.supermap.dataservice.cloud.key;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.key.KeyWordService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年07月19 14:37
 * @email 1301584033@qq.com
 */
@Service
public class KeyWordServiceImpl implements KeyWordService {

    @Autowired
    private KeyWordMapper keyWordMapper;

    @Override
    public JSONUtils getSysKeyWordList(String areaCode, String typeCode, Integer state) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(keyWordMapper.getSysKeyWordList(areaCode,typeCode,state));
    }

    @Override
    public Map<String, Object> getKeyWord(String str) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        Map<String, Object> map= keyWordMapper.getKeyWord(str);
        return keyWordMapper.getKeyWord(str);
    }

    @Override
    public JSONUtils delKeyWord(Integer id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(keyWordMapper.delKeyWord(id));
    }

    @Override
    public JSONUtils putKeyWord(Integer userId,String typeCode,Integer typeId,String typeTitle,String content,String keyword,String keygroup,String datetime) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
          return new JSONUtils().setData(keyWordMapper.putKeyWord(userId,typeCode,typeId,typeTitle,content,keyword,keygroup,datetime));
    }

    @Override
    public JSONUtils upState(Integer keyId, Integer state, String autoContent,String auditorId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
        String userName=keyWordMapper.getUserName(auditorId);
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(keyWordMapper.upState(keyId,state,autoContent,userName,auditorId));
    }

    @Override
    public JSONUtils upKeyWord(String typeCode, Integer typeId,Integer del) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if(Strings.isNotBlank(typeCode)){
            if(typeCode.equals("nq")){
                keyWordMapper.delNq(typeId,del);
            }
            if(typeCode.equals("nqhd")){
                keyWordMapper.delNqHd(typeId,del);
            }
            if(typeCode.equals("zq")){
                keyWordMapper.delZq(typeId,del);
            }
            if(typeCode.equals("zqhd")){
                keyWordMapper.delZqHd(typeId,del);
            }
            if(typeCode.equals("zx")){
                keyWordMapper.delZx(typeId,del);
            }
            if(typeCode.equals("zxhd")){
                keyWordMapper.delZxHd(typeId,del);
            }
            if(typeCode.equals("zj")){
                keyWordMapper.delJy(typeId,del);
            }

        }
        return new JSONUtils();
    }

    @Override
    public Integer getLastId() {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return keyWordMapper.getLastId();
    }
}
